package com.xinbochuang.machine.admin.service.impl.system;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.domain.system.SysDictData;
import com.xinbochuang.machine.admin.mapper.system.SysDictDataMapper;
import com.xinbochuang.machine.admin.service.system.ISysDictDataService;
import com.xinbochuang.machine.common.utils.DictUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author 黄晓鹏
 * @date 2020-09-18
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {

    @Resource
    private SysDictDataMapper dictDataMapper;

    /**
     * 根据条件分页查询字典数据
     *
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public IPage<SysDictData> selectDictDataList(SysDictData dictData) {
        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByAsc(SysDictData::getDictSort);
        if (dictData != null) {
            if (StrUtil.isNotBlank(dictData.getDictType())) {
                queryWrapper.lambda().like(SysDictData::getDictType, dictData.getDictType());
            }
            if (StrUtil.isNotBlank(dictData.getDictLabel())) {
                queryWrapper.lambda().like(SysDictData::getDictLabel, dictData.getDictLabel());
            }
            if (StrUtil.isNotBlank(dictData.getStatus())) {
                queryWrapper.lambda().eq(SysDictData::getStatus, dictData.getStatus());
            }
        }
        return page(new Page<>(dictData.getPageNum(), dictData.getPageSize()), queryWrapper);
    }

    /**
     * 新增保存字典数据信息
     *
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public boolean insertDictData(SysDictData dictData) {
        boolean result = save(dictData);
        if (result) {
            DictUtils.clearDictCache();
        }
        return result;
    }

    /**
     * 修改保存字典数据信息
     *
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public boolean updateDictData(SysDictData dictData) {
        boolean result = updateById(dictData);
        if (result) {
            DictUtils.clearDictCache();
        }
        return result;
    }

    /**
     * 批量删除字典数据信息
     *
     * @param dictCodes 需要删除的字典数据ID
     * @return 结果
     */
    @Override
    public boolean deleteDictDataByIds(Integer[] dictCodes) {
        boolean result = removeByIds(Arrays.asList(dictCodes));
        if (result) {
            DictUtils.clearDictCache();
        }
        return result;
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        return list(Wrappers.<SysDictData>lambdaQuery().eq(SysDictData::getDictType, dictType).eq(SysDictData::getStatus, 0).orderByAsc(SysDictData::getDictSort));
    }
}
